package com.jurenzhao.partssystem.business.order_enterprise.controller;

import com.jurenzhao.partssystem.accessories.analysis.entity.Piece_Info;
import com.jurenzhao.partssystem.accessories.analysis.service.Piece_InfoMapperSerevice;
import com.jurenzhao.partssystem.business.order_enterprise.entity.SaleOrder;
import com.jurenzhao.partssystem.business.order_enterprise.entity.TemporaryList;
import com.jurenzhao.partssystem.business.order_enterprise.entity.TemporaryOrder;
import com.jurenzhao.partssystem.business.order_enterprise.service.SaleOrderService;
import com.jurenzhao.partssystem.business.order_enterprise.service.TemporaryOrderService;
import com.jurenzhao.partssystem.information.basic_model.entity.BasicModel;
import com.jurenzhao.partssystem.information.basic_model.service.BasicModelService;
import com.jurenzhao.partssystem.information.basic_series.entity.BasicSeries;
import com.jurenzhao.partssystem.information.basic_series.service.BasicSeriesService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("/sale_order")
public class SaleOrderController {

    @Resource
    private SaleOrderService saleOrderService;

    @Resource
    private TemporaryOrderService temporaryOrderService;

    @Resource
    Piece_InfoMapperSerevice piece_infoMapperSerevice;

    @Resource
    private BasicModelService basicModelService;

    @Resource
    private BasicSeriesService basicSeriesService;

    @RequestMapping("/orderlist")
    public String Orderlist(Model model){
        List<Piece_Info> list = piece_infoMapperSerevice.findAll();
        List<BasicModel> modelLists = basicModelService.findAll();
        List<BasicSeries> seriesList = basicSeriesService.findAll();
        TemporaryList temporaryList = new TemporaryList();
        temporaryList.setPieceInfoList(list);
        temporaryList.setBasicModelList(modelLists);
        temporaryList.setBasicSeriesList(seriesList);
        model.addAttribute("temporaryList",temporaryList);
        return "Business/Order_Enterprise/Temporary_Order_List";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<SaleOrder> saleOrderList = saleOrderService.list();
        model.addAttribute("saleOrderList",saleOrderList);
        return "Business/Order_Enterprise/Sale_Order_List";
    }
    
    @RequestMapping("/delete/{orderId}")
    public String delete(@PathVariable String orderId){
        saleOrderService.delete(orderId);
        return "redirect:/sale_order/list";
    }

    @RequestMapping("/preadd")
    public String preadd(){
        return "Business/Order_Enterprise/Sale_Order_Add";
    }

    @RequestMapping("/add")
    public String save(SaleOrder saleOrder){
        Date d = new Date();
        String orderID =String.valueOf(d.getTime());
        //System.out.println("-------------------"+orderID);
        Date date = new Date();
        saleOrder.setOrderId(orderID);
        saleOrder.setOrderDate(date);
        System.out.println(saleOrder);
        saleOrderService.save(saleOrder);
        return "redirect:/sale_order/list";
    }

    @RequestMapping("/preupdate/{orderId}")
    public String preupdate(@PathVariable String orderId){
        SaleOrder saleOrder = saleOrderService.selectByPrimaryKey(orderId);
        return "Business/Order_Enterprise/Sale_Order_Update";
    }
}
